package com.elinkway.infinitemovies.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.ChannelNewBean;
import com.elinkway.infinitemovies.bigdata.BigDataApi;
import com.elinkway.infinitemovies.bigdata.BigDataConstants;
import com.elinkway.infinitemovies.config.SettingManage;
import com.elinkway.infinitemovies.selfdata.MyDataRecordCreater;
import com.elinkway.infinitemovies.selfdata.bean.MyDataRecord;
import com.elinkway.infinitemovies.selfdata.dao.RealmDaoManager;
import com.elinkway.infinitemovies.ui.activity.ChannelDetailActivity;
import com.elinkway.infinitemovies.ui.activity.LoginActivity;
import com.elinkway.infinitemovies.ui.activity.NewMainActivity;
import com.elinkway.infinitemovies.ui.activity.TopicDetailActivity;
import com.elinkway.infinitemovies.ui.activity.VideoDetailActivity;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.Utils;
import com.le123.ysdq.R;

import java.util.List;
import java.util.Map;

/**
 * Created by zhaopeipei on 2016/6/2.
 */
public class ChannelAdapter extends MyBaseAdapter {

    private List<ChannelNewBean> mChannelsList;

    private Activity mActivity;

    private boolean isHome;

    private SharedPreferences mLoginSp;

    private class GridItemHolder {
        TextView title;
    }

    public ChannelAdapter(Context context, List<ChannelNewBean> mChannelsList, boolean isHome) {
        super(context);
        mActivity = (Activity) context;
        this.mChannelsList = mChannelsList;
        this.isHome = isHome;
        mLoginSp = mActivity.getSharedPreferences(SettingManage.LOGIN_INFO, Context.MODE_PRIVATE);
    }

    public ChannelAdapter(Context context, List<ChannelNewBean> mChannelsList) {
        super(context);
        mActivity = (Activity) context;
        this.mChannelsList = mChannelsList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ChannelNewBean bean = mChannelsList.get(position);
        GridItemHolder holder;
        if (convertView == null) {
            if(isHome){
                convertView = inflater.inflate(R.layout.channel_gridview_home_item,parent,false);
            }else{
                convertView = inflater.inflate(R.layout.channel_gridview_item,parent,false);
            }
            holder = new GridItemHolder();
            holder.title = (TextView) convertView
                    .findViewById(R.id.tv_channel_gridview_item);
            convertView.setTag(holder);
        } else {
            holder = (GridItemHolder) convertView.getTag();
        }
        holder.title.setText(bean.getName());
        TextPaint tp = holder.title.getPaint();
        tp.setFakeBoldText(true);
        holder.title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Map<String, String> dataMap = BigDataApi.createDataMap("0", "channelID="+bean.getId(),"0","-","","-");
                if(mActivity instanceof NewMainActivity){
                    dataMap.put("cur_url", ((NewMainActivity)mActivity).getCurrentPageId());
                }
                BigDataApi.bigDataCommonReport(dataMap, mActivity);

                if (bean.getType().equals("1")) {
                    Map<String, String> map = BigDataApi.createDataMap("0", "channel=" + bean.getName()
                            +"&subchannel=" + bean.getId(),"0","-","","-");
                    map.put("cur_url", BigDataConstants.PGV_CURRENT_ID_CATEGORY);
                    if (mLoginSp != null) {
                        map.put("uid", mLoginSp.getString(LoginActivity.UID, ""));
                    }
                    map.put("ilu", mLoginSp == null || TextUtils.isEmpty(mLoginSp.getString(LoginActivity.UID, "")) ? "0" : "1");
                    BigDataApi.bigDataCommonReport(map, mActivity);
                    bean.getParam().setName(bean.getName());
                    LogUtils.d("channelF" , "channel.getSubName() is:" + bean.getName());
                    MyDataRecord record = MyDataRecordCreater.build(MyDataRecord.class);
                    record.setCur_url("category");
                    record.setAcode("0");
                    record.setTag_name(bean.getName());
                    RealmDaoManager.addMyDataRecord(record);
                    ChannelDetailActivity.launch(mActivity, bean.getParam().getVt(),
                            bean.getParam().getTitle(), bean.getParam());
                } else if (bean.getType().equals("2")) {
                    TopicDetailActivity.launch(mActivity, bean.getParam().getThemeId(), bean.getParam().getTitle());
                } else if (bean.getType().equals("3")) {
                    VideoDetailActivity.launch(mActivity, bean.getParam().getAid(),
                            bean.getParam().getVt(), bean.getParam().getTitle(), "", bean.getParam().getThemeId(), "", "0","","");
                } else if (bean.getType().equals("4")) {
                    Utils.jumpWebViewActivity(mActivity, bean.getParam().getUrl(),
                            bean.getParam().getTitle());
                } else if (bean.getType().equals("501")) {

                } else if (bean.getType().equals("502")) {

                } else if (bean.getType().equals("503")) {
//                    RankDetailActivity.launch(mActivity, bean.getParam().getVt());
                    if (mActivity != null && mActivity instanceof NewMainActivity) {
                        ((NewMainActivity)mActivity).setBottomSelete(4);
                    }
                } else if (bean.getType().equals("504")) {
                    if (mActivity != null && mActivity instanceof NewMainActivity) {
                        ((NewMainActivity) mActivity).setBottomSelete(3);
                    }
                }
            }
        });
        return convertView;
    }

    @Override
    public int getCount() {
        if (mChannelsList != null) {
            return mChannelsList.size();
        } else {
            return 0;
        }
    }

}
